package org.yoyo.classroom.service.impl;

import org.yoyo.classroom.domain.po.Permission;
import org.yoyo.classroom.domain.query.UserQuery;
import org.yoyo.classroom.mapper.PermissionMapper;
import org.yoyo.classroom.mapper.UserMapper;
import org.yoyo.classroom.domain.po.User;
import org.yoyo.classroom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户业务逻辑层实现类
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据ID查询用户
     * @param id 用户ID
     * @return 用户对象
     */
    @Override
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }

    /**
     * 查询所有用户
     * @return 用户列表
     */
    @Override
    public List<User> getAllUsers() {
        return userMapper.selectAll();
    }

    /**
     * 插入用户
     * @param user 用户对象
     * @return 影响的行数
     */
    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    /**
     * 更新用户
     * @param user 用户对象
     * @return 影响的行数
     */
    @Override
    public int updateUser(User user) {
        user.setUpdateTime(new Date());
        return userMapper.update(user);
    }

    /**
     * 删除用户（逻辑删除）
     * @param id 用户ID
     * @return 影响的行数
     */
    @Override
    public int deleteUser(Long id) {
        return userMapper.delete(id);
    }


    @Override
    public List<Permission> findPermissionsByUsername(String username) {
        return permissionMapper.findPermissionsByUsername(username);
    }

    @Override
    public List<User> getUsers(UserQuery query) {
        return userMapper.selectByQuery(query);
    }
}
